//============================================================================
// Name        : proxy_pattern.cpp
// Author      : Tony
// Version     :
// Copyright   : GPLv2
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;
class Image
{
public:
	virtual ~Image(){}
	virtual void displayImage() = 0;
};

class RealImage : public Image
{
public:
	RealImage(string filename)
	{
		this->filename = filename;
		cout<<"construct RealImage "<<filename<<endl;
	}
	virtual void displayImage()
	{
		cout<<"display RealImage "<<filename<<endl;
	}
private:
	string filename;
};

class ImageProxy : public Image
{
public:
	ImageProxy(string filename)
	{
		this->filename = filename;
		this->realImage = NULL;
	}
	virtual ~ImageProxy()
	{
		delete realImage;
	}
	virtual void displayImage()
	{
		if (this->realImage == NULL)
		{
			this->realImage = new RealImage(filename);
		}
		this->realImage->displayImage();
	}
private:
	string filename;
	RealImage* realImage;
};


int main() {
	Image* image1 = new ImageProxy("image1.jpg");
	Image* image2 = new ImageProxy("image2.jpg");
	Image* image3 = new ImageProxy("image3.jpg");
	image1->displayImage();
	image2->displayImage();
	image1->displayImage();

	delete image1;
	delete image2;
	delete image3;
	return 0;
}
